/*
 * Copyright (C) 1994-2021 Altair Engineering, Inc.
 * For more information, contact Altair at www.altair.com.
 *
 * This file is part of both the OpenPBS software ("OpenPBS")
 * and the PBS Professional ("PBS Pro") software.
 *
 * Open Source License Information:
 *
 * OpenPBS is free software. You can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 *
 * OpenPBS is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Commercial License Information:
 *
 * PBS Pro is commercially licensed software that shares a common core with
 * the OpenPBS software.  For a copy of the commercial license terms and
 * conditions, go to: (http://www.pbspro.com/agreement.html) or contact the
 * Altair Legal Department.
 *
 * Altair's dual-license business model allows companies, individuals, and
 * organizations to create proprietary derivative works of OpenPBS and
 * distribute them - whether embedded or bundled with other software -
 * under a commercial license agreement.
 *
 * Use of Altair's trademarks, including but not limited to "PBS™",
 * "OpenPBS®", "PBS Professional®", and "PBS Pro™" and Altair's logos is
 * subject to Altair's trademark licensing policies.
 */

/*	pbsD_modify_resv.c
 *
 *	The Modify Reservation request.
 */

#include <pbs_config.h> /* the master config generated by configure */

#include "dis.h"
#include "libpbs.h"
#include "net_connect.h"
#include "pbs_ecl.h"
#include "pbs_idx.h"
#include "pbs_share.h"
#include "portability.h"
#include "tpp.h"
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

/**
 * @brief
 *	-Pass-through call to send preempt jobs batch request
 *
 * @param[in] connect - connection handler
 * @param[in] preempt_jobs_list - list of jobs to be preempted
 *
 * @return      preempt_job_info *
 * @retval      list of jobs and their preempt_method
 * @retval		NULL in case of error
 *
 */
static preempt_job_info *
PBSD_preempt_jobs(int connect, char **preempt_jobs_list)
{
	struct batch_reply *reply = NULL;
	preempt_job_info *ppj_reply = NULL;
	preempt_job_info *ppj_temp = NULL;
	int rc = -1;

	DIS_tcp_funcs();

	/* first, set up the body of the Preempt Jobs request */

	if ((rc = encode_DIS_ReqHdr(connect, PBS_BATCH_PreemptJobs, pbs_current_user)) ||
	    (rc = encode_DIS_JobsList(connect, preempt_jobs_list, -1)) ||
	    (rc = encode_DIS_ReqExtend(connect, NULL))) {
		if (set_conn_errtxt(connect, dis_emsg[rc]) != 0) {
			pbs_errno = PBSE_SYSTEM;
			return NULL;
		}
		if (pbs_errno == PBSE_PROTOCOL)
			return NULL;
	}
	if (dis_flush(connect)) {
		pbs_errno = PBSE_PROTOCOL;
		return NULL;
	}

	reply = PBSD_rdrpy(connect);
	if (reply == NULL)
		pbs_errno = PBSE_PROTOCOL;
	else {
		int i = 0;
		int count = 0;
		ppj_temp = reply->brp_un.brp_preempt_jobs.ppj_list;
		count = reply->brp_un.brp_preempt_jobs.count;

		ppj_reply = calloc(sizeof(struct preempt_job_info), count);
		if (ppj_reply == NULL)
			return NULL;

		for (i = 0; i < count; i++) {
			strcpy(ppj_reply[i].job_id, ppj_temp[i].job_id);
			strcpy(ppj_reply[i].order, ppj_temp[i].order);
		}
		PBSD_FreeReply(reply);
	}
	return ppj_reply;
}

/**
 * @brief
 *	-Pass-through call to send preempt jobs batch request
 *
 * @param[in] c - connection handler
 * @param[in] preempt_jobs_list - list of jobs to be preempted
 *
 * @return	preempt_job_info *
 * @retval	list of jobs and their preempt_method
 * @retval	NULL for Error/Failure
 *
 */
preempt_job_info *
__pbs_preempt_jobs(int c, char **preempt_jobs_list)
{
	preempt_job_info *ret = NULL;

	/* initialize the thread context data, if not already initialized */
	if (pbs_client_thread_init_thread_context() != 0)
		return NULL;

	/* lock pthread mutex here for this connection
	 * blocking call, waits for mutex release */
	if (pbs_client_thread_lock_connection(c) != 0)
		return NULL;

	ret = PBSD_preempt_jobs(c, preempt_jobs_list);

	/* unlock the thread lock and update the thread context data */
	if (pbs_client_thread_unlock_connection(c) != 0)
		return NULL;

	return ret;
}
